package com.lovesher.payment.service.utils.excel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtils {
	
	public static BigDecimal isNull(BigDecimal money){
		if(null==money){
			return BigDecimal.ZERO;
		}
		return money;
	}
	/**
	 * 保留两位小数
	 * @param money
	 * @return
	 */
	public static String format2Str(BigDecimal money){
		String str = "0.00";
		if(null==money){
			return str;
		}
		str = money.setScale(2, BigDecimal.ROUND_FLOOR).toPlainString();
		return str;
	}
	
	/**
	 * 格式化小数
	 * @param d
	 * @return
	 */
	public static String fomatNum(Double d){
	      String str="";
	      DecimalFormat df=new DecimalFormat("###,###0.00");//设置输出数值的格式为XX.XX
	      str=df.format(d);
	      return str;

	}
	
	/**
	 * 格式化小数
	 * @param d
	 * @return
	 */
	public static String fomatNum(BigDecimal bigDecimal){
	      String str="";
	      DecimalFormat df=new DecimalFormat("###,###");//设置输出数值的格式为XX.XX
	      str=df.format(bigDecimal);
	      return str;
	}
	public static double format(double d, String format) {
		DecimalFormat df = new DecimalFormat(format);
		String ds = df.format(d);
		return Double.parseDouble(ds);
	}

	public static double format2(double d) {
		DecimalFormat df = new DecimalFormat("0.00");
		String ds = df.format(d);
		return Double.parseDouble(ds);
	}

	private static final DecimalFormat df2 = new DecimalFormat("0.00");

	/**
	 * 保留两位小数，四舍五入
	 * 
	 * @param d
	 * @return
	 */
	/*
	 * public synchronized static String format2Str(double d){ String
	 * ds=df2.format(d); return ds; }
	 */

	/**
	 * 截取到小数点后两位，不进行四舍五入
	 * 
	 * @param d
	 * @return
	 */
	public synchronized static String format2Str(double d) {
		df2.setRoundingMode(RoundingMode.FLOOR);
		String ds = df2.format(d);
		return ds;
	}

	public static double format4(double d) {
		DecimalFormat df = new DecimalFormat("0.0000");
		String ds = df.format(d);
		return Double.parseDouble(ds);
	}

	public static double format6(double d) {
		DecimalFormat df = new DecimalFormat("0.000000");
		String ds = df.format(d);
		return Double.parseDouble(ds);
	}

	public static BigDecimal format7(BigDecimal d) {
		DecimalFormat df = new DecimalFormat("0.000000");
		String ds = df.format(d);
		return new BigDecimal(ds);
	}

	public static double getDouble(String str) {
		if (str == null || str.equals(""))
			return 0.0;
		double ret = 0.0;
		try {
			ret = Double.parseDouble(str);
		} catch (NumberFormatException e) {
			ret = 0.0;
		}
		return format6(ret);
	}

	public static BigDecimal getBigDecimal(String str) {
		if (str == null || str.equals(""))
			return BigDecimal.ZERO;
		return new BigDecimal(str);
	}

	public static long getLong(String str) {
		if (str == null || str.equals(""))
			return 0L;
		long ret = 0;
		try {
			ret = Long.parseLong(str);
		} catch (NumberFormatException e) {
			ret = 0;
		}
		return ret;
	}

	public static Long[] getLongs(String[] str) {
		if (str == null || str.length < 1)
			return new Long[0];
		Long[] ret = new Long[str.length];
		for (int i = 0; i < str.length; i++) {
			ret[i] = getLong(str[i]);
		}
		return ret;
	}

	// 截取数字
	public static long getStrNumber(String content) {
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			return getLong(matcher.group(0));
		}
		return 0;
	}

	public static int getInt(String str) {
		if (str == null || str.equals(""))
			return 0;
		int ret = 0;
		try {
			ret = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			ret = 0;
		}
		return ret;
	}

	public static int compare(double x, double y) {
		BigDecimal val1 = new BigDecimal(x);
		BigDecimal val2 = new BigDecimal(y);
		return val1.compareTo(val2);
	}

	/**
	 * 两个浮点型数据相加的精确算法
	 * 
	 * @param x
	 * @param y
	 * @return x+y
	 */
	public static double add(double x, double y) {
		BigDecimal xb = new BigDecimal(x + "");
		BigDecimal yb = new BigDecimal(y + "");
		return xb.add(yb).doubleValue();
	}

	/**
	 * 两个浮点型数据相减的精确算法
	 * 
	 * @param x
	 * @param y
	 * @return x-y
	 */
	public static double subStract(double x, double y) {
		BigDecimal xb = new BigDecimal(x + "");
		BigDecimal yb = new BigDecimal(y + "");
		return xb.subtract(yb).doubleValue();
	}

	/**
	 * 两个浮点型数据相乘的精确算法
	 * 
	 * @param x
	 * @param y
	 * @return x*y
	 */
	public static double multiply(double x, double y) {
		BigDecimal xb = new BigDecimal(x + "");
		BigDecimal yb = new BigDecimal(y + "");
		return xb.multiply(yb).doubleValue();
	}

	/**
	 * 两个浮点型数据相除的精确算法
	 * 
	 * @param x
	 * @param y
	 * @return x/y
	 */
	public static double divide(double x, double y) {
		BigDecimal xb = new BigDecimal(x + "");
		BigDecimal yb = new BigDecimal(y + "");
		return xb.divide(yb, 6, BigDecimal.ROUND_HALF_UP).doubleValue();// 保留六位小数，第七位四舍五入进位
	}

	/**
	 * @param d
	 * @param len
	 * @return
	 */
	public static double ceil(double d, int len) {
		/*
		 * String str=Double.toString(d); int a=str.indexOf(".");
		 * if(a+3>str.length()){ a=str.length(); }else{ a=a+3; }
		 * str=str.substring(0, a); return Double.parseDouble(str);
		 */
		DecimalFormat a = new DecimalFormat("#,##0.000000");
		String frmStr = a.format(d);
		String str = frmStr.substring(0, frmStr.indexOf(".") + 3);
		return Double.parseDouble(str);
	}

	public static double ceil(double d) {
		return ceil(d, 2);
	}

	/**
	 * 保留两位小数(向上取整)
	 * @param money
	 * @return
	 */
	public static String format2UpStr(BigDecimal money){
		String str = "0.00";
		if(null==money){
			return str;
		}
		str = money.setScale(2, BigDecimal.ROUND_UP).toPlainString();
		return str;
	}
	/**
	 * 保留两位小数ROUND_FLOOR
	 * @param money
	 * @return
	 */
	public static String format2FloorStr(BigDecimal money){
		String str = "0.00";
		if(null==money){
			return str;
		}
		str = money.setScale(2, BigDecimal.ROUND_FLOOR).toPlainString();
		return str;
	}
	
	/**
	 * 获取带","的金额字符串
	 * 如 10000 ==> 10,000
	 * @param money
	 * @param scale
	 */
	public static String getMoneyStr(BigDecimal money, int scale){
		if(null == money){
			return "0";
		}
		String numStr = money.setScale(scale, BigDecimal.ROUND_UP).toPlainString();
		String str[] = numStr.split("\\.");
		int count = 0;
		StringBuffer newNum = new StringBuffer("");
		for (int i = str[0].length() - 1; i >= 0; i--) {
			count++;
			newNum.insert(0, str[0].charAt(i));
			if (count % 3 == 0 && i > 0) {
				newNum.insert(0, ",");
			}
		}
		newNum.append(".").append(str[1]);
		return newNum.toString();
	}
	
	/**
	 * 根据精度取值(截取)
	 * @param money
	 * @param scale
	 */
	public static String formatStr(BigDecimal money, int scale){
		String str = "0";
		if(null==money){
			return str;
		}
		str = money.setScale(scale, BigDecimal.ROUND_DOWN).toPlainString();
		return str;
	}
	
	/**
	 * @description 金钱格式化，千分位分隔，保留指定小数位数
	 * @param money 待格式化的金额
	 * @param scale 指定需要保留的小数位数
	 * @return
	 */
	public static String formatMoney(BigDecimal money,int scale,RoundingMode roundingMode){
		String moneyStr = "";
		if(null == money){
			return moneyStr;
		}
			BigDecimal fmoney = money.setScale(scale, roundingMode);
		StringBuffer f = new StringBuffer(",##0");
		for(int i=1;i<=scale;i++){
			if(1==i){
				f.append(".");
			}
			f.append("0");
		}
		DecimalFormat df = new DecimalFormat(f.toString());
		moneyStr = df.format(fmoney);
		return moneyStr;
	}

	/**
	 * BigDecimal相加(空值判断)
	 * 
	 * @param x
	 * @param y
	 * @return x + y
	 */
	public static BigDecimal add(BigDecimal x, BigDecimal y) {
		if(null == x){
			x = BigDecimal.ZERO;
		}
		if(null == y){
			y = BigDecimal.ZERO;
		}
		return x.add(y);
	}
	

	/**
	 * BigDecimal减法(空值判断)
	 * 
	 * @param x
	 * @param y
	 * @return x - y
	 */
	public static BigDecimal sub(BigDecimal x, BigDecimal y) {
		if(null == x){
			x = BigDecimal.ZERO;
		}
		if(null == y){
			y = BigDecimal.ZERO;
		}
		return x.subtract(y);
	}
	
	/**
	 * BigDecimal先加再减(空值判断)
	 * 
	 * @param x
	 * @param y
	 * @param z
	 * @return x + y - z
	 */
	public static BigDecimal addAndSub(BigDecimal x, BigDecimal y, BigDecimal z) {
		if(null == x){
			x = BigDecimal.ZERO;
		}
		if(null == y){
			y = BigDecimal.ZERO;
		}
		if(null == z){
			z = BigDecimal.ZERO;
		}
		return x.add(y).subtract(z);
	}
	
}
